﻿using System;
using System.Data;
using System.Collections.Generic;
using YXShop.Model.Admin;
using YXShop.DALFactory;
using YXShop.IDAL.Admin;

namespace YXShop.BLL.Admin
{
    /// <summary>
    /// 业务逻辑类Administrators 的摘要说明。
    /// </summary>
    public class Administrators
    {
        private readonly IAdministrators dal = DataAccess.CreateAdministrators();

        public Administrators()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string adminName)
        {
            return dal.Exists(adminName);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(YXShop.Model.Admin.Administrators model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(YXShop.Model.Admin.Administrators model)
        {
            dal.Update(model);
        }

        public int Amend(int id, string columnName, Object value)
        {
            return dal.Amend(id,columnName,value);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int adminid)
        {

            dal.Delete(adminid);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public YXShop.Model.Admin.Administrators GetModel(int adminid)
        {

            return dal.GetModel(adminid);
        }

        /// <summary>
        /// 通过管理员的帐号获取实例
        /// </summary>
        /// <param name="amdinName"></param>
        /// <returns></returns>
        public YXShop.Model.Admin.Administrators GetModelByAdminName(string amdinName)
        {
            return dal.GetModelByAdminName(amdinName);
        }

        /// <summary>
        /// 生成List
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public List<YXShop.Model.Admin.Administrators> GetList(System.Data.DataTable dt)
        {

            return dal.GetList(dt);
        }

        public string GetList()
        {
            return dal.GetList();
        }
        public ChangeHope.DataBase.DataByPage GetList(string where)
        {
            return dal.GetList(where);
        }
        public ChangeHope.DataBase.DataByPage GetListDB()
        {
            return dal.GetListDB();
        }
        /// <summary>
        /// 获得该用户管理的销售中心，前提条件是用户是销售中心管理员
        /// </summary>
        /// <param name="adminid"></param>
        /// <returns></returns>
        public List<YXShop.Model.Store.StoreCenter> GetStoreCenterAdmin_StoreCenter(int adminid)
        {
            YXShop.BLL.Store.StoreCenter storeBll = new BLL.Store.StoreCenter();
            List<YXShop.Model.Store.StoreCenter> stores = storeBll.GetListShopByWhere(" and adminid=" + adminid.ToString());
            return stores;
        }
        /// <summary>
        /// 获得该用户管理的门店，前提条件是用户是销售中心管理员
        /// </summary>
        /// <param name="adminid"></param>
        /// <returns></returns>
        public List<YXShop.Model.Store.Store> GetStoreCenterAdmin_Store(int adminid)
        {
            //先找到StoreCenter
            YXShop.BLL.Store.StoreCenter storeCenterBll = new BLL.Store.StoreCenter();
            List<YXShop.Model.Store.StoreCenter> storeCenters = storeCenterBll.GetListShopByWhere(" and adminid=" + adminid.ToString());
            List<YXShop.Model.Store.Store> stores = new List<Model.Store.Store>();
            if (storeCenters.Count > 0)
            {
                List<int> ids=new List<int> ();
                for (int i = 0; i < storeCenters.Count; i++)
                {
                    ids.Add(storeCenters[i].Sid.Value);
                }
                string strWhere = ChangeHope.Common.Util.GetWhereInClause(ids, "SID");
                YXShop.BLL.Store.Store storeBLL=new Store.Store ();
                stores = storeBLL.GetListShopByWhere(strWhere);
            }

            return stores;
        }
        /// <summary>
        /// 获得该用户管理的门店，前提条件是用户是门店管理员
        /// </summary>
        /// <param name="adminid"></param>
        /// <returns></returns>
        public List<YXShop.Model.Store.Store> GetStoreAdmin_Store(int adminid)
        {
            YXShop.BLL.Store.Store storeBll = new BLL.Store.Store();
            List<YXShop.Model.Store.Store> stores = storeBll.GetListShopByWhere(" and adminid=" + adminid.ToString());
            return stores;
        }
        /// <summary>
        /// 获得该用户管理的门店所属的管理中心，前提条件是用户是门店管理员
        /// </summary>
        /// <param name="adminid"></param>
        /// <returns></returns>
        public List<YXShop.Model.Store.StoreCenter> GetStoreCenterOfAdmin_Store(int adminid)
        {
            List<YXShop.Model.Store.StoreCenter> result = new List<Model.Store.StoreCenter>();
            //先取得该用户管理的门店
            List<YXShop.Model.Store.Store> stores = GetStoreAdmin_Store(adminid);
            //根据这些门店获取他们的销售中心ID
            List<int> ids = new List<int>();
            foreach (YXShop.Model.Store.Store store in stores)
            {
                ids.Add(store.Storecenterid.Value);
            }
            string strWhere = ChangeHope.Common.Util.GetWhereInClause(ids, "sid");
            YXShop.BLL.Store.StoreCenter centerBLL = new Store.StoreCenter();
            result = centerBLL.GetListShopByWhere(strWhere);
            return result;
        }
        #endregion  成员方法
    }
}

